package com.nuoniu.sibanyun.controller.erp.barcode;


import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpNsInOutMain;
import com.nuoniu.sibanyun.entity.so.VAreaCalculationMain;
import com.nuoniu.sibanyun.service.erp.barcode.IErpNsInOutMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nuoniu.sibanyun.common.BaseController;

/**
 * <p>
 * ERP非标 出入库主表 前端控制器
 * </p>
 *
 * @author 白强
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/erp/nsInOutMain")
@Api(value="ERP非标 出入库主表", tags={"ERP非标 出入库主表"})
public class ErpNsInOutMainController extends BaseController {


    @Autowired
    private IErpNsInOutMainService erpNsInOutMainService;

    /**
     * 保存
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public HttpResult save(@RequestBody ErpNsInOutMain ErpNsInOutMain) {
        if(ErpNsInOutMain == null || ErpNsInOutMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }
        erpNsInOutMainService.saveErpNsInOutMain(ErpNsInOutMain);
        return HttpResult.ok();
    }

    /**
     * 获取单据编码
     */
    @ApiOperation("获取单据编码")
    @GetMapping("getCode")
    public HttpResult getCode(String sourceCode) {
        UserInfo userInfo = UserThreadLocal.get();
        return HttpResult.ok(erpNsInOutMainService.getCode(userInfo.getCompanyId(),sourceCode));
    }

    @DeleteMapping("delete")
    @ApiOperation("删除单据")
    public HttpResult delete(Long id) {
        erpNsInOutMainService.deleteById(id);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核/弃审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        erpNsInOutMainService.examine(id,status);
        return HttpResult.ok();
    }

    @GetMapping("saveFromBarcode")
    @ApiOperation("扫描出库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mainId", value = "发货单主表ID"),
            @ApiImplicitParam(name = "barcode", value = "条码（一个）")
    })
    public HttpResult saveFromBarcode(Long mainId,String barcode) {
        return HttpResult.ok(erpNsInOutMainService.saveFromBarcode(mainId, barcode));
    }




}
